package com.carleasoft.mps.cms.business.fileUpload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.fileUpload.bean.entity.InformationLibrary;
import com.carleasoft.mps.cms.business.fileUpload.bean.entity.InformationTypeEntity;
import com.carleasoft.mps.cms.business.fileUpload.bean.po.InformationLibraryPo;
import com.carleasoft.mps.cms.business.fileUpload.bean.po.LabelVo;
import com.carleasoft.mps.cms.business.fileUpload.bean.vo.InformationLibraryVo;
import com.carleasoft.mps.cms.business.fileUpload.dao.InformationLibraryMapper;
import com.carleasoft.mps.cms.business.fileUpload.service.InformationLibraryService;
import com.carleasoft.mps.cms.business.fileUpload.service.InformationTypeService;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yanghao
 * @date 2022-02-17 16:41
 * @project mps
 */
@Service
public class InformationLibraryServiceImpl extends ServiceImpl<InformationLibraryMapper, InformationLibrary> implements InformationLibraryService {


    @Autowired
    private InformationTypeService informationTypeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(InformationLibraryPo informationLibraryPo, UserCacheData userInfo) {
        InformationLibrary informationLibrary = ConvertUtils.sourceToTarget(informationLibraryPo, InformationLibrary.class);
        informationLibrary.setAccessoryCode(UUID.randomUUID().toString());
        informationLibrary.setIsTop(2);
        informationLibrary.setUserCode(userInfo.getUserCode());
        informationLibrary.setUserName(userInfo.getUserName());
        informationLibrary.setCreateTime(LocalDateTime.now());
        boolean isOk = this.save(informationLibrary);
        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateInformation(InformationLibraryPo informationLibraryPo, UserCacheData user) {
        InformationLibrary informationLibrary = this.getOne(Wrappers.<InformationLibrary>lambdaQuery()
                .eq(InformationLibrary::getAccessoryCode, informationLibraryPo.getAccessoryCode()));
        BeanUtil.copyProperties(informationLibraryPo, informationLibrary);
        informationLibrary.setUpdateTime(LocalDateTime.now());
        informationLibrary.setUserName(user.getUserName());
        informationLibrary.setUserCode(user.getUserCode());
        informationLibrary.setCreateTime(LocalDateTime.now());
        return this.updateById(informationLibrary);
    }

    @Override
    public Page pageQuery(Page page, InformationLibraryPo informationLibraryPo) {
        Page<InformationLibraryVo> informationLibraryVoPage = this.getBaseMapper().pageQuery(page, informationLibraryPo);
        if (CollUtil.isNotEmpty(informationLibraryVoPage.getRecords())) {
            List<String> typeCode = informationLibraryVoPage.getRecords().stream().map(InformationLibraryVo::getTypeCode).collect(Collectors.toList());
            List<InformationTypeEntity> informationTypeEntities = informationTypeService.list(Wrappers.<InformationTypeEntity>lambdaQuery()
                    .in(InformationTypeEntity::getInformationTypeCode, typeCode));
            if (CollUtil.isNotEmpty(informationTypeEntities)) {
                Map<String, InformationTypeEntity> map = informationTypeEntities.stream().collect(Collectors.toMap(InformationTypeEntity::getInformationTypeCode, Function.identity()));
                for (InformationLibraryVo record : informationLibraryVoPage.getRecords()) {
                    if (map.containsKey(record.getTypeCode())) {
                        record.setInformationTypeName(map.get(record.getTypeCode()).getInformationTypeName());
                    }
                }
            }

        }
        return informationLibraryVoPage;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id, UserCacheData userInfo) {
        boolean isOk = false;
        InformationLibrary informationLibrary = this.getOne(new QueryWrapper<InformationLibrary>().eq("ACCESSORY_CODE", id), false);
        if (Objects.nonNull(informationLibrary)) {
            isOk = this.removeById(informationLibrary.getId());
        }
        return isOk;
    }

    @Override
    public List<LabelVo> labelList() {
        Map<String, LabelVo> labelVos = new HashMap<>();
        List<InformationLibrary> libraryList = this.list();
        if (CollUtil.isNotEmpty(libraryList)) {
            for (InformationLibrary informationLibrary : libraryList) {
                if (StrUtil.isNotBlank(informationLibrary.getLabel())) {
                    LabelVo labelVo = new LabelVo();
                    labelVo.setLabel(informationLibrary.getLabel());
                    labelVo.setId(informationLibrary.getId());
                    labelVos.put(informationLibrary.getLabel(), labelVo);
                }
            }
        }
        return new ArrayList<>(labelVos.values());
    }

    @Override
    public Page<InformationLibraryVo> eipPage(Page page, InformationLibraryPo informationLibraryPo) {
        return this.getBaseMapper().eipPage(page, informationLibraryPo);


    }

    @Override
    public InformationLibrary selectById(InformationLibraryPo informationLibraryPo) {
        return this.getOne(Wrappers.<InformationLibrary>lambdaQuery().eq(InformationLibrary::getAccessoryCode, informationLibraryPo.getAccessoryCode()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean isTop(InformationLibraryPo informationLibraryPo) {
        return this.update(Wrappers.<InformationLibrary>lambdaUpdate()
                .eq(InformationLibrary::getId, informationLibraryPo.getId())
                .set(InformationLibrary::getIsTop, informationLibraryPo.getIsTop()));
    }


}
